package com.syni.mdd.yhd.common.all.service.impl.record;

import com.syni.mdd.yhd.common.all.constants.Constants;
import com.syni.mdd.yhd.common.all.dao.jpa.record.ReGoupChatDao;
import com.syni.mdd.yhd.common.all.dao.jpa.record.ReOrderRefundLogDao;
import com.syni.mdd.yhd.common.all.entity.record.ReOrderRefundLog;
import com.syni.mdd.yhd.common.all.service.impl.BaseService;
import com.syni.mdd.yhd.common.all.service.record.ReOrderRefundLogService;
import com.syni.mdd.yhd.common.all.utils.SpecificationUtils;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @className
 * @description TOO
 * @Author cfx
 * @DATE 2019/4/9 12:04
 * @VERSION 1.0
 **/
@Service
public class ReOrderRefundLogServiceImpl extends BaseService<ReOrderRefundLog, Integer> implements ReOrderRefundLogService {

    @Autowired
    ReOrderRefundLogDao reOrderRefundLogDao;

    @Override
    public ReOrderRefundLogDao getBaseDao() {
        return reOrderRefundLogDao;
    }

    @Override
    public void addRefundLogRecordByAli(Integer orderId, Double refundMoney, String requestNo) {
        Map<String,Object> conditionMap = new HashMap<>();
        conditionMap.put("orderId",orderId);
        conditionMap.put("refundMoney",refundMoney);
        conditionMap.put("requestNo",requestNo);
        conditionMap.put("refundChannel",ReOrderRefundLog.ALI_PAY);
        List<ReOrderRefundLog> reOrderRefundLogs = reOrderRefundLogDao.findAll(new SpecificationUtils(conditionMap));
        if(!CollectionUtils.isNotEmpty(reOrderRefundLogs)){
            ReOrderRefundLog reOrderRefundLog = new ReOrderRefundLog();
            reOrderRefundLog.setOrderId(orderId);
            reOrderRefundLog.setRefundMoney(refundMoney);
            reOrderRefundLog.setRequestNo(requestNo);
            reOrderRefundLog.setNewTime(new Date());
            reOrderRefundLog.setRefundChannel(ReOrderRefundLog.ALI_PAY);
            reOrderRefundLogDao.save(reOrderRefundLog);
        }
    }

    @Override
    public void addRefundLogRecordByWx(Integer orderId, Double refundMoney, String refundId) {
        Map<String,Object> conditionMap = new HashMap<>();
        conditionMap.put("orderId",orderId);
        conditionMap.put("refundMoney",refundMoney);
        conditionMap.put("refundId",refundId);
        conditionMap.put("refundChannel",ReOrderRefundLog.WX_PAY);
        List<ReOrderRefundLog> reOrderRefundLogs = reOrderRefundLogDao.findAll(new SpecificationUtils(conditionMap));
        if(!CollectionUtils.isNotEmpty(reOrderRefundLogs)){
            ReOrderRefundLog reOrderRefundLog = new ReOrderRefundLog();
            reOrderRefundLog.setOrderId(orderId);
            reOrderRefundLog.setRefundMoney(refundMoney);
            reOrderRefundLog.setRefundId(refundId);
            reOrderRefundLog.setNewTime(new Date());
            reOrderRefundLog.setRefundChannel(ReOrderRefundLog.WX_PAY);
            reOrderRefundLogDao.save(reOrderRefundLog);
        }
    }

    @Override
    public void updateAliRefundSatus(Integer orderId, String requestNo) {
        Map<String,Object> conditionMap = new HashMap<>();
        conditionMap.put("orderId",orderId);
        conditionMap.put("requestNo",requestNo);
        conditionMap.put("refundChannel",ReOrderRefundLog.ALI_PAY);
        List<ReOrderRefundLog> reOrderRefundLogs = reOrderRefundLogDao.findAll(new SpecificationUtils(conditionMap));
        if(CollectionUtils.isNotEmpty(reOrderRefundLogs)){
            ReOrderRefundLog reOrderRefundLog = reOrderRefundLogs.get(0);
            reOrderRefundLog.setStatus(Constants.COMMON_FLAG_TRUE_INT);
            this.getBaseDao().save(reOrderRefundLog);
        }
    }
}
